import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
import { resolve } from "path";
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons';

function _resolve(dir: string) {
  return resolve(__dirname, dir);
}

export default ({ mode }) => {
  const env = loadEnv(mode, process.cwd(), '');
  const { APP_BASEURL } = env;

  return defineConfig({
    plugins: [
      vue(),
      AutoImport({
        resolvers: [ElementPlusResolver()],
      }),
      Components({
        resolvers: [ElementPlusResolver()],
      }),
      createSvgIconsPlugin({
        // 配置路径在你的src里的svg存放文件
        iconDirs: [resolve(process.cwd(), 'src/assets/icons')],
        symbolId: 'icon-[dir]-[name]',
      }),
    ],
    resolve: {
      alias: {
        '@': _resolve('src'),
      },
    },
    css: {
      preprocessorOptions: {
        scss: {
          javascriptEnabled: true,
          additionalData: "@import '@/assets/styles/main.scss';",
        },
      },
    },
    server: {
      proxy: {
        '/api': {
          target: APP_BASEURL,
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, '')
        },
        '/socket': {
          target: APP_BASEURL,
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/socket/, ''),
          ws: true,
        },
      },
    },
  });
}